# -*- coding: utf-8 -*-

from __future__ import absolute_import

import sys

from PyQt4.QtGui import QApplication
from PyQt4.QtGui import QMainWindow
from PyQt4.QtGui import QSplashScreen
from PyQt4.QtGui import QIcon
from PyQt4.QtGui import QPixmap
from PyQt4.QtGui import QToolBar

from PyQt4.QtCore import Qt
from PyQt4.QtCore import QCoreApplication
from PyQt4.QtCore import SIGNAL

from ninja_ide import resources
from ninja_ide.gui import updates
from ninja_ide.gui import status_bar
from ninja_ide.gui import main_window
#TODO: imports menus
from ninja_ide.whatever import styles


###############################################################################
# IDE: MAIN CONTAINER
###############################################################################


class IDE(QMainWindow):

    __instance = None
    max_opacity = 1
    min_opacity = 0.3

    def __new__(cls, *args, **kargs):
        if cls.__instance is None:
            cls.__instance = QMainWindow.__new__(cls, args, kargs)
        return cls.__instance

    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle('NINJA-IDE {Ninja Is Not Just Another IDE}')
        self.setWindowIcon(QIcon(resources.IMAGES['icon']))
        self.setWindowState(Qt.WindowMaximized)
        self.setMinimumSize(700, 500)

        #Opactity
        self.opacity = self.max_opacity

        #ToolBar
        self._toolbar = QToolBar()
        self._toolbar.setToolTip(self.tr("Press and Drag to Move"))
        styles.set_style(self._toolbar, 'toolbar')
        #TODO
        #Waiting to define Settings Location
#        self.addToolBar(settings.value('preferences/interface/toolbar',
#            Qt.LeftToolBarArea).toInt()[0], self._toolbar)
        self._toolbar.setToolButtonStyle(Qt.ToolButtonIconOnly)

        #StatusBar
        self._status = status_bar.StatusBar()
        self._status.hide()
        self.setStatusBar(self._status)

        #Main Widget
        self._main = main_window.MainWindow(self)
        self.setCentralWidget(self._main)

        #Menu
        menubar = self.menuBar()
        file_ = menubar.addMenu(self.tr("&File"))
        edit = menubar.addMenu(self.tr("&Edit"))
        view = menubar.addMenu(self.tr("&View"))
        source = menubar.addMenu(self.tr("&Source"))
        project = menubar.addMenu(self.tr("&Project"))
        pluginsMenu = menubar.addMenu(self.tr("P&lugins"))
        about = menubar.addMenu(self.tr("&About"))

        #The order of the icons in the toolbar is defined by this calls
        #TODO
#        self._menuFile = MenuFile(file_, self._toolbar)
#        self._menuView = MenuView(view, self)
#        self._menuEdit = MenuEdit(edit, self._toolbar)
#        self._menuSource = MenuSource(source, self)
#        self._menuProject = MenuProject(project, self._toolbar)
#        self._menuPlugins = MenuPlugins(pluginsMenu, self)
#        self._menuAbout = MenuAbout(about)

        # Analyze this code
#        self._main.container.load_toolbar(self._toolbar)
#        self._main._central.actual_tab().obtain_editor().setFocus()

        #Tray Icon
        #TODO (Core section)
#        self.trayIcon = updates.TrayIconUpdates(self)
#        self.trayIcon.show()


###############################################################################
# START NINJA-IDE
###############################################################################


def start():
    app = QApplication(sys.argv)
    QCoreApplication.setOrganizationName('NINJA-IDE')
    QCoreApplication.setOrganizationDomain('ninja-ide.org.ar')
    QCoreApplication.setApplicationName('Kunai')

    # Create and display the splash screen
    splash_pix = QPixmap(resources.IMAGES['splash'])
    splash = QSplashScreen(splash_pix, Qt.WindowStaysOnTopHint)
    splash.setMask(splash_pix.mask())
    splash.show()
    app.processEvents()

    #Loading GUI
    splash.showMessage("Loading GUI", Qt.AlignRight | Qt.AlignBottom, Qt.white)
    ide = IDE()

    ide.show()
    splash.finish(ide)
    sys.exit(app.exec_())
